import __config from '../config/env'
const request = (url, data) => {
  let showLoading = true;
  let _url = __config.basePath + url
  // console.log('requestURL=========='+_url)
  // console.log('requestData============='+JSON.stringify(data))
  return new Promise((resolve, reject) => {
    if (showLoading){
      wx.showLoading({
        title: '加载中',
        mask:true
      })
    }
    wx.request({
      url: _url,
      method: 'get',
      data: data,
      header: {
        //设置参数内容类型为x-www-form-urlencoded
        'content-type':'application/x-www-form-urlencoded',
        'Accept': 'application/json',
        'cookie':wx.getStorageSync("LtpaToken")||''
      },
      success(res) {
        if (res.statusCode == 200) {
          if (res.data.code != 200) {
            console.log(res.data)
            wx.showModal({
              title: '提示',
              content: res.data.message ? res.data.message : '没有数据' + '',
              showCancel:false,
              success() {
                
              },
              complete(){
                
              }
            })
            reject(res.data.msg)
          }
          resolve(res.data)
        } else if (res.statusCode == 404) {
          wx.showModal({
            title: '提示',
            content: '接口请求出错，请检查手机网络',
            showCancel:false,
            success(res) {
            }
          })
          reject()
        } else {
          console.log(res)
          wx.showModal({
            title: '提示',
            content: res.errMsg + ':' + res.data.message + ':' + res.data.msg,
            showCancel:false,
            success(res) {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }
          })
          reject()
        }
      },
      fail(error) {
        console.log(error)
        wx.showModal({
          title: '提示',
          content: '接口请求出错：' + error.errMsg,
          showCancel:false,
          success(res) {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }
        })
        reject(error)
      },
      complete(res) {
        wx.hideLoading()
      }
    })
  })
}
const post = (url, data) => {
  let showLoading = true;
  let _url = __config.basePath + url
  // console.log('requestURL=========='+_url)
  // console.log('requestData============='+JSON.stringify(data))
  return new Promise((resolve, reject) => {
    if (showLoading){
      wx.showLoading({
        title: '加载中',
        mask:true
      })
    }
    wx.request({
      url: _url,
      method: 'POST',
      data: data,
      header: {
        'content-type':'application/json',
        'Accept': 'application/json',
      },
      success(res) {
        if (res.statusCode == 200) {
          if (res.data.code != 200) {
            console.log(res.data)
            wx.showModal({
              title: '提示',
              content: res.data.message ? res.data.message : '网络异常' + '',
              showCancel:false,
              success() {
                
              },
              complete(){
                
              }
            })
            reject(res.data.msg)
          }
          resolve(res.data)
        } else if (res.statusCode == 404) {
          wx.showModal({
            title: '提示',
            content: '接口请求出错，请检查手机网络',
            showCancel:false,
            success(res) {
            }
          })
          reject()
        } else {
          console.log(res)
          wx.showModal({
            title: '提示',
            content: res.errMsg + ':' + res.data.message + ':' + res.data.msg,
            showCancel:false,
            success(res) {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }
          })
          reject()
        }
      },
      fail(error) {
        console.log(error)
        wx.showModal({
          title: '提示',
          content: '接口请求出错：' + error.errMsg,
          showCancel:false,
          success(res) {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }
        })
        reject(error)
      },
      complete(res) {
        wx.hideLoading()
      }
    })
  })
}
module.exports = {
  request,
  login: (code) => {//小程序登录接口
    return request('/api/user/login', code)
  },
  getTask: (openId) => {//获取用户任务
    return request('/api/task/get', openId)
  },
  getLastTask: (openId) => {//获取用户任务
    return request('/api/task/getlast', openId)
  },
  getEndTask: (openId) => {//获取用户任务
    return request('/api/task/getend', openId)
  },
  saveUser:(data) => {//保存用户接口
    return post('/api/user/save',data)
  },
  saveTask:(data) => {//保存任务接口
    return post('/api/task/save',data)
  },
  getHistory:(data) => {//保存探访记录
    return request('/api/task/findAll',data)
  },
  getShow:() => {//保存探访记录
    return request('/api/show/get',{})
  }
}